<template>
  <div class='container'>
    <div class="directorys-container">
    <el-card>
      <div slot="header" v-if="Object.keys(subject).length !== 0" ref="showheader">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row>
        <el-col :span="18" >
          <el-form ref="directorysForm" :model="directorysInfo"  style="display: flex;">
              <el-form-item label="目录名称" prop="directoryName"  label-width="80px">
                <el-input v-model="directorysInfo.directoryName"  />
              </el-form-item>
              <el-form-item label="状态：" prop="state" label-width="80px">
                  <el-select v-model="directorysInfo.state" >
                    <el-option
                      v-for="item in status"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              <el-form-item>
                <el-button size="small"  style="margin-left: 10px;" @click="resetForm">清除</el-button>
                <el-button size="small" type="primary" style="margin-left: 10px;" @click="handleFilter">搜索</el-button>
              </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right;">
          <el-row>
            <el-col :span="24" >
              <el-button type="text" size="small" @click="$router.push('list')" v-if="Object.keys(subject).length !== 0">
                <i class="el-icon-back"></i>
                <span>返回学科</span>
              </el-button>
              <el-button type="success" size="small" @click="AddDirectorysFn('add')">
                <i class="el-icon-edit"></i>
                <span>新增目录</span>
              </el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <!-- 警告信息 -->
      <el-alert
        :title="directorystitle"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px;"
      />
      <el-table
      :data="dataList"
      v-loading="listLoading"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科"
        width="241">
      </el-table-column>
      <el-table-column
        prop="directoryName"
        label="目录名称"
        width="238">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者"
        width="238">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期"
        width="238">
        <template #default="{row}">
          <!-- {{row}} -->
          {{ formatTimeFn(row.addDate) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="totals"
        label="面试题数量"
        width="238">
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        width="238">
        <template #default="{row}">
          {{row.state? '屏蔽中' : '启用中'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        width="150">
        <template #default="{row}">
          <el-button
            type="text"
            size="small"
            @click="EnableFn(row)"
          >{{row.state? '启用':'禁用'}}</el-button>
          <el-button
            type="text"
            size="small"
            :disabled="Boolean(row.state)"
            @click="AddDirectorysFn(row)"
          >修改</el-button>
          <el-button
            type="text"
            size="small"
            :disabled="Boolean(row.state)"
            @click="delDirectorys(row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div style="height: 60px; margin-top: 10px; display:flex;justify-content:flex-end">
      <el-pagination
        small
        background
        layout="sizes, prev, pager, next, jumper"
        :total="counts"
        :current-page="requestParameters.page"
        :page-size="requestParameters.pagesize"
        :page-sizes="[5,10,15,20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    </el-card>
    <directorys-add ref="directorys" :formData.sync="formData" :dialogVisible.sync="dialogVisible" :dialogTitle="dialogTitle"  />
    </div>
  </div>
</template>

<script>
import { list, remove, detail, changeState } from '@/api/hmmm/directorys'
import { status } from '@/api/hmmm/constants'
import dayjs from 'dayjs'
import DirectorysAdd from '@/module-hmmm/components/directorys-add.vue'
export default {
  name: '',
  components: { DirectorysAdd },
  data () {
    return {
      /** 搜索框 */
      directorysInfo: {
        directoryName: '',
        subjectID: ''
      },
      directorystitle: '数据一共xx条', // 轻警告提示
      requestParameters: {
        page: 1, // 当前页码
        pagesize: 10 // 单页条数
      },
      counts: 0, // 总条数
      dataList: [], // table 展示数据
      dialogVisible: false,
      dialogTitle: '新增学科',
      listLoading: false,
      disabled: false,
      showheader: false,
      formData: {}
    }
  },
  computed: {
    status () {
      return status
    },
    subject () {
      return this.$route.query || {}
    }
  },
  watch: {
    '$route.query': function (to, form) {
      this.directorysListApi({ subjectID: this.subject.id })
    }
  },
  created () {
    this.directorysListApi()
  },
  methods: {
    /** 获取列表数据，进行页面展示 */
    async directorysListApi (data) {
      this.listLoading = true
      const { data: { counts, items } } = await list({ ...this.requestParameters, ...data })
      this.counts = counts
      this.dataList = items
      this.directorystitle = '数据一共' + counts + '条'
      this.listLoading = false
    },
    /** 是否启用 */
    EnableFn (data) {
      let str = ''
      if (data.state === 1) {
        str = '启用'
        data.state = 0
      } else {
        str = '禁用'
        data.state = 1
      }
      const obj = {
        id: data.id,
        state: data.state
      }
      this.$confirm('你确定要' + str + '禁用吗', '提示')
        .then(async () => {
          await changeState(obj)
          this.$message.success(str + '成功')
        })
        .catch(() => {
          if (data.state === 0) {
            data.state = 1
          } else {
            data.state = 0
          }
          this.$message.success(str + '已取消')
        })
    },
    // 重置
    resetForm () {
      this.$refs.directorysForm.resetFields()
    },
    // 搜索信息
    handleFilter () {
      this.requestParameters.page = 1
      const { directoryName, state } = this.directorysInfo
      if (directoryName.length && (state || !state)) this.directorysListApi({ directoryName, state })
      if (directoryName.length) this.directorysListApi({ directoryName })
      state ? this.directorysListApi({ state }) : this.directorysListApi({ state })
    },
    /** 自定义序号 */
    indexMethod (index) {
      return (this.requestParameters.page - 1) * this.requestParameters.pagesize + index + 1
    },
    /** 入职时间格式处理 */
    formatTimeFn (data) {
      return dayjs(data).format('YYYY-MM-DD hh:mm:ss')
    },
    /** 分页---每页条目发送变化时 */
    handleSizeChange (val) {
      // console.log(val)
      this.requestParameters.pagesize = val
      this.requestParameters.page = 1
      this.directorysListApi(this.requestParameters)
    },
    /** 分页---页码发生变化时 */
    handleCurrentChange (val) {
      // console.log(val)
      this.requestParameters.page = val
      this.directorysListApi(this.requestParameters)
    },
    /** 打开添加学科 dialog */
    AddDirectorysFn (data) {
      this.dialogVisible = true
      if (data === 'add') {
        this.dialogTitle = '新增目录'
      } else {
        this.dialogTitle = '编辑目录'
        this.editSubjects(data)
      }
    },
    /** 删除目录 */
    delDirectorys (data) {
      this.$confirm('你确定要删除该学科吗？', '提示')
        .then(async () => {
          await remove(data)
          this.$message.success('学科删除成功')
          this.directorysListApi()
        })
        .catch(() => {})
    },

    /** 获取学科详情 */
    async editSubjects (data) {
      const { data: res } = await detail({ id: data.id })
      this.formData = res
    }
  }

}
</script>

<style lang="scss" scoped>
.container {
  .directorys-container{
    // padding: 10px;
    .el-card{
      .el-row:after {
        clear: both;
      }
      .el-row:before {
        display: table;
      }
      .el-form-item--medium {
    margin-bottom: 18px;
}
    }
  }
}
</style>
